/**************************************************************************
	Replication do-file: "Corruption in Customs"
	Cyril Chalendard, Ana Fernandes, Gael Raballand and Bob Rijkers
	
	Created on: 01/07/2022
**************************************************************************/

clear all
set more off, perm
cap log close
cls

* ----------------------- DIRECTORIES AND FOLDERS ----------------------- *

* Directories in which data are stored
global main "PUT YOUR DIRECTORY PATH HERE"
cd "$folder"

* Folders
global outputdata = "$main\Output Data"
cap mkdir "$main\Tables"
global tables = "$main\Tables"

* ----------------------------- BEGINS HERE ----------------------------- *

* -------
* Dataset
* -------
use "$outputdata\CFRR.dta", clear // open dataset

* Sample
keep if sample_int==1 & eis_f!=. // keep observations of interest


* ---------
* Table A10
* ---------

* Globals
global dep "ln_time fraud c1_dlog_val c1_dlog_tax htrl_f_int"
global controls "tax_rate risk red_i mixed_dec differentiated valitrade_advice"

* Matrix
mat tableA10 = J(10,5,.)
mat tableA10_T = J(1,5,.)

* Loop for estimations
local i = 1
local j = 1
foreach x in $dep {
	
	qui: reghdfe `x' eis_nr_f $controls countryorig* max_twodigit* if sample_reg==1 , absorb(insp_f key_dec ym_*) vce(cluster insp_f key_dec)
	
	mat tableA10[`i',`j'] = _b[eis_nr_f]
	local i = `i' + 1
	mat tableA10[`i',`j'] = _se[eis_nr_f]
	mat tableA10_T[1,`j'] = e(N)
	local i = `i' + 8
	mat tableA10[`i',`j'] = e(r2)
	local i = `i' -9
	local j = `j' +1
}


* Export
* ------
putexcel set "$tables\Table A10.xlsx", replace sheet(TableA10) // create a new excel spreadsheet

* Coefficients
putexcel B4 = matrix(tableA10), nformat(0.000) hcenter vcenter
putexcel B12 = matrix(tableA10_T), nformat(number_sep) hcenter vcenter

* Title
putexcel (A1:F1), merge hcenter vcenter
putexcel A1 = "Before delegated randomization of inspector assignment"

* Column names
putexcel A2 = "Dependent variable:", left vcenter bold
local cells `" "B" "C" "D" "E" "F" "'
local labels `" "Time" "Fraud" "\Delta log value" "\Delta log tax" "Hyp. tax revenue losses" "'
forvalues i = 1(1)5 {
	
	local x: word `i' of `labels'
	local y: word `i' of `cells'
	
	putexcel `y'2 = "`x'", hcenter vcenter bold	
	putexcel `y'3 = "(`i')", hcenter vcenter
	
	forvalues j = 6(1)11 {
		putexcel `y'`j' = "Yes", hcenter vcenter	
	}
}

* Row names
putexcel A4 = "Excess interaction share - inspector logits", left vcenter
local j = 6
local names `" "Declaration characteristics" "Inspector fixed effects" "Broker fixed effects" "Source country fixed effects" "HS2-product fixed effects" "Month-year fixed effects" "'
forvalues i = 1(1)6 {
	
	local x: word `i' of `names'
	
	putexcel A`j' = "`x'", left vcenter
	local j = `j'+1
}
putexcel A12 = "Observations", left vcenter
putexcel A13 = "R-squared", left vcenter


* -------------------------------- ENDS HERE -------------------------------- *